/**
 * @class common.base.selector
 * @extends common.base.object
 * 选择弹层基类
 */
define(function (require) {

    var Pagination = require('common/pagination'),
        Uploader = require('common/uploader'),
        utils = require('common/utils/usercenter'),
        Base = require('common/base/tabModal');

    return Base.extend({
        default: {},
        /**
         * 构造函数
         * @param  {Object} params 配置参数
         */
        initialize: function (params) {
            this.list_params = {p: 1};
            this.name = this.type + '_selector';
            if (_.isFunction(params))params = {success: params};
            params.multi_selectable = params.multi_selectable == true;
            Base.prototype.initialize.apply(this, [params]);
            this.showContent();
        },
        /**
         * 获取列表数据
         * @param  {Object} params 配置参数
         * @param {Function} callback 回调参数
         */
        fetch: function (params, callback) {
            var _this = this,
                listTab = _.get('$.tabs[?(@.name=="' + _this.current_tab_name + '")]', _this.params);
            _this.list_params = params;
            utils.ajaxGet(_.extend(params, { p: _.intval(params.p), size: 30}, _this.getFetchParams()),
                function (data) {
                    if (listTab.parse) {
                        data.info.list = listTab.parse(data.info.list);
                    }
                    _this.$(' .modal-body').removeClass('loading');
                    _this.data = _.extend({tab_name: _this.current_tab_name + '_item'}, data.info, _this.params);
                    _this.$('.modal-body .list_wrap').html(_this.render(_this.tpl, _this.data));
                    _this.$('.modal-body').css({'min-height': _this.$('.modal-body').height() + 'px'})
                    _this.bindListEvents();
                    (new Pagination({wrap: _this.$('.modal-footer .pagination_wrap'), style: 'left'})).show(_this.data.page, _this.fetch, _this);
                    if (callback) {
                        callback.apply(_this, [data.info]);
                    }
                }
            );
        },
        bindListEvents: function () {
            var _this = this;
            _this.$('.select_all').click(function () {
                _this.$('.item :checkbox').prop('checked', $(this).is(':checked')).uniform();
            });
            function _get_item_data($li) {
                var id = $li.data('id'),
                    jsonPath = '$.list[?(@.id==' + id + ')]';
                return {
                    data_id: id,
                    data_type: _this.type,
                    data_content: _this.getResult(_.get(jsonPath, _this.data)),
                    model: _.get(jsonPath, _this.data)
                };
            }

            _this.$('.modal-body .list_wrap .select').click(function () {
                if (_this.params.multi_selectable) {
                    var $checkbox = $(':checkbox', $(this));
                    $checkbox.attr('checked', !($checkbox.attr('checked') == 'checked')).uniform();
                } else {
                    _this.params.success.apply(_this, [_get_item_data($(this).parents('.item'))]);
                    _this.close();
                }
            });
            _this.$('.btn-submit').unbind('click').click(function () {
                var result = [];
                _this.$('.list_wrap .item :checkbox:checked').each(function () {
                    result.push(_get_item_data($(this).parents('.item')));
                });
                _this.params.success.apply(_this, [result]);
                _this.close();
            });
        },
        /**
         * 初始化swfupload，继承类如果需要上传就需要执行该函数
         * @param {String} type 上传类型，如:image,voice,video
         */
        initUpload: function (type) {
            var _this = this,
                type = _.empty(type) ? _this.type : type;
            _this.showContent();
            var upload = new Uploader({
                url: _.U({m: 'assets', a: 'upload', type: type}),
                wrap: _this.$('.swfupload'),
                file_type: type,
                max_size: _this.params.max_size,
                success: function (data) {
                    _this.upload_result = data;
                    if (type == 'image') {
                        var cid = _this.$('.JCate').select2('val');
                        utils.ajaxPost({m: 'assets', a: 'edit', id: data.data_id, cid: cid}, function () {
                            _this.$('.upload_result_wrap').html('<img src="' + _.attach(data.data_content) + '" width="100" height="50">');
                        });
                    }
                }
            });
            _this.$('.action_wrap .btn-submit').click(function () {
                if (!_.empty(_this.upload_result)) {
                    _this.params.success.apply(_this,
                        _this.params.multi_selectable ? [
                            [_this.upload_result]
                        ] : [_this.upload_result]
                    );
                }
                _this.close();
            });
        },
        /**
         * 设置请求参数，继承类一般需要重载该函数
         */
        getFetchParams: function () {
            return this.params.fetch_params;
        },
        /**
         * 解析list中的item用来设置data_content内容，继承类一般需要重载该函数
         * @param  {Object} item 一个item
         */
        getResult: function (result) {
            return result;
        }
    });
});